package view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JSlider;

import listener.RotateGlassPaneListener;
import utils.ImageUtils;

/**
 * Glass pane using for rotation
 * 
 * @author Maxime
 * 
 */
public class RotateGlassPane extends KoalaGlassPane {
	private JButton m_rotateLeft;
	private JButton m_rotateRight;
	private JSlider m_sliderRotation;
	private JButton m_confirmButton;
	private JButton m_cancelButton;
	private int m_angle;
	private BufferedImage m_preview;

	public RotateGlassPane() {
		super();
		m_angle = 0;
		m_rotateLeft = new JButton("-90");
		m_rotateRight = new JButton("+90");
		// TODO mettre image button rotate degres
		m_sliderRotation = new JSlider(-180, 180, m_angle);
		m_confirmButton = new JButton(new ImageIcon(this.getClass()
				.getResource("/images/" + "valid.png")));
		m_cancelButton = new JButton(new ImageIcon(this.getClass().getResource(
				"/images/" + "cancel.png")));

		m_rotateLeft.setName("rotate_left");
		m_rotateRight.setName("rotate_right");
		m_confirmButton.setName("confirm");
		m_cancelButton.setName("cancel");

		add(m_rotateLeft);
		add(m_rotateRight);
		add(m_sliderRotation);
		add(m_confirmButton);
		add(m_cancelButton);

		m_rotateLeft.addActionListener(new RotateGlassPaneListener());
		m_rotateRight.addActionListener(new RotateGlassPaneListener());
		m_sliderRotation.addChangeListener(new RotateGlassPaneListener());
		m_confirmButton.addActionListener(new RotateGlassPaneListener());
		m_cancelButton.addActionListener(new RotateGlassPaneListener());
		this.addMouseListener(new RotateGlassPaneListener());
		m_confirmButton.setVisible(true);
		m_cancelButton.setVisible(true);
		repaint();
	}

	public void setAngle(int angle) {
		m_angle = angle;
	}

	public int getAngle() {
		return m_angle;
	}

	@Override
	public void paintComponent(Graphics g) {
	
		BufferedImage bImg = new BufferedImage(getWidth(), getHeight(),
				BufferedImage.TYPE_INT_ARGB);

		for (int x = 0; x < getWidth(); x++) {
			for (int y = 0; y < getHeight(); y++) {
				bImg.setRGB(x, y, new Color(255, 255, 255, 100).getRGB());
			}
		}

		int imageLeftPointX = MainFrame.getInstance().getDrawingPane()
				.getSelectedLayer().getX()
				+ MainFrame.getInstance().getDrawingPane().getX() + 3;

		int imageLeftPointY = (int) (MainFrame.getInstance().getDrawingPane()
				.getSelectedLayer().getY()
				+ MainFrame.getInstance().getDrawingPane().getY()
				+ MainFrame.getInstance().getMainPanel().getY()
				+ MainFrame.getInstance().getScrollingPane().getY() + 3);

		BufferedImage imageSelected = MainFrame.getInstance().getDrawingPane()
				.getSelectedLayer().getImage();
		m_preview = ImageUtils.deepCopy(imageSelected);

		int imageWidth = m_preview.getWidth();
		int imageHeight = m_preview.getHeight();

		g.drawImage(bImg, 0, 0, getWidth(), getHeight(), this);

		AffineTransform affineTransform = new AffineTransform();

		affineTransform.translate(imageLeftPointX + (imageWidth / 2),
				imageLeftPointY + (imageHeight / 2));
		affineTransform.rotate(Math.toRadians(m_angle));
		int centreX = imageSelected.getWidth(this) / 2;
		int centreY = imageSelected.getHeight(this) / 2;
		affineTransform.translate(-centreX, -centreY);
		((Graphics2D) g).drawImage(m_preview, affineTransform, this);
		super.paintComponent(g);
	}

	public JSlider getSliderRotation() {
		return m_sliderRotation;
	}

	public void setSliderRotation(JSlider sliderRotation) {
		this.m_sliderRotation = sliderRotation;
	}

}
